package com.springboot.application.controller;

import cn.hutool.json.JSONUtil;
import com.springboot.application.entity.UserEntity;
import com.springboot.application.service.UserService;
import org.json.JSONObject;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author zhengzihong
 * @version 1.0.0
 * @date 2018/11/1
 * @Description
 */
@RestController
public class UserController {


    @Resource
    private UserService userService;

    @RequestMapping(value = "/getUserInfo", method = RequestMethod.GET)
    public String getUserInfo(HttpServletRequest request) {
        String id = request.getParameter("id");
        if (StringUtils.isEmpty(id)) {
            return "入参为空";
        }

        UserEntity userEntity = userService.getUserInfoById(Long.valueOf(id));
        if (userEntity != null) {
            JSONObject jsonObject = new JSONObject(userEntity);
            return jsonObject.toString();
        }
        return "根据id:" + id + "获取不到用户信息";
    }


    @PostMapping(value = "/getUserInfoByCondition")
    public String getUserInfoByCondition(@RequestBody UserEntity request) {
        UserEntity userEntity = userService.getUserInfo(request);
        if (userEntity != null) {
            JSONObject jsonObject = new JSONObject(userEntity);
            return jsonObject.toString();
        }
        return "根据条件:" + JSONUtil.toJsonStr(request) + "获取不到用户信息";
    }
}
